from basepage import BasePage
from .. import widgets

class GroupMasters(BasePage):
    fader_size = dict(w=.07, h=.4)
    label_size = dict(w=.07, h=.025)
    page_name = 'Group Masters'
    def __init__(self, **kwargs):
        super(GroupMasters, self).__init__(**kwargs)
        self.num_masters = kwargs.get('num_masters', 8)
        self.x_offset = kwargs.get('x_offset', 0)
        self.y_offset = kwargs.get('y_offset', 0)
        w = self.fader_size['w'] * self.num_masters
        h = self.fader_size['h'] + self.label_size['h']
        bounds = [self.x_offset, self.y_offset, w, h]
        self.build_topwidget('Label', name='GroupMasters', bounds=bounds, value='')
        self.masters = {}
        self.build_widgets()
        self.topwidget.refresh_interface()
        colors = {}
        for i in range(3):
            fg = [0]*3
            fg[i] = 255
            bg = [0]*3
            bg[i] = 80
            colors[i] = [bg, fg, [128]*3]
        #self.testmulti.set_child_colors(colors)
        #slider = self.masters[1].slider
        #css = ', '.join([widgets.make_css(l).join(["'", "'"]) for l in colors])
        #slider._interface_update('runScript', "%s.setColors([%s])" % (slider.id, css), add_widget_id=False)
        
    def build_widgets(self):
        for i in range(1, self.num_masters+1):
            m = GroupMaster(parent=self, index=i)
            self.masters[i] = m
        #self.testmulti = self.topwidget.add_widget(widgets.MultiSlider, name='testmulti', bounds=[0, .5, .5, .5])
        

class GroupMaster(object):
    def __init__(self, **kwargs):
        self.parent = kwargs.get('parent')
        self.iOsc = self.parent.iOsc
        self.MainController = self.iOsc.MainController
        self.index = kwargs.get('index')
        #kwargs.update({'osc_address':str(self.index), 'osc_parent_node':self.parent.osc_node})
        #super(GroupMaster, self).__init__(**kwargs)
        
        
        self.group = None
        
        self.x_offset = self.parent.x_offset
        self.y_offset = self.parent.y_offset
        
        self.w = self.parent.label_size['w']
        self.x = ((self.index-1) * self.w) + self.x_offset
        
        bounds = [self.x, self.y_offset, self.w, self.parent.topwidget.bounds[2]]
        self.topwidget = self.parent.topwidget.add_widget(widgets.Label, 
                                                          name=str(self.index), 
                                                          bounds=bounds)
        
        h = self.parent.label_size['h']
        lbl_kwargs = dict(x=self.x, y=self.y_offset, width=self.w, height=h, 
                          foregroundColor=[255, 255, 255], backgroundColor=[128, 128, 128, 128])
                          #name='index_label', value=str(self.index))
        #self.index_label = self.topwidget.add_widget(widgets.Label, **lbl_kwargs)
        name = ''
        lbl_kwargs.update(dict(y=self.y_offset, value=' ', name='name_label'))
        self.name_label = self.topwidget.add_widget(widgets.Label, **lbl_kwargs)
        
        bounds = [self.x, self.y_offset + self.name_label.height, self.w, self.parent.fader_size['h']]
        self.slider = self.topwidget.add_widget(widgets.VSlider, name='Fader', bounds=bounds, update_label_with_value=True, 
                                                add_label=True, add_value_label=True, label=str(self.index), 
                                                foregroundColor=[0, 255, 0], 
                                                backgroundColor=[128, 128, 128, 255])#, use_int_value=True)
        
        #lbl_kwargs.update(dict(y=y+self.slider.height, name='GroupMaster_Label_Dimmer_'+str(self.index)))
        #self.dimmer_label = self.iOsc.add_widget('Label', **lbl_kwargs)
        
        self.MainController.Groups.bind(child_update=self.on_mc_groups_child_update)
        
        #self.MainController.connect('new_group', self.on_mc_new_group)
        group = self.MainController.Groups.indexed_items.get(self.index)
        if group:
            self.set_group(group)
            
    def unlink(self):
        self.MainController.Groups.unbind(self.on_mc_groups_child_update)
        self.set_group(None)
        super(GroupMaster, self).unlink()
    
    def set_group(self, group):
        if self.group:
            self.group.unbind(self.on_group_property_changed)
            #self.slider.set_src_object()
            #self.dimmer_label.set_src_object()
        self.group = group
        if self.group:
            self.name_label.value = group.name
            self.slider.Property = (group.AttributeGroups['Dimmer'], 'value')
            group.bind(property_changed=self.on_group_property_changed)
        else:
            self.name_label.value = ' '
            #self.dimmer_label.value = ' '
            self.slider.Property = None
            self.slider.value = 0
        
    def on_group_property_changed(self, **kwargs):
        prop = kwargs.get('Property')
        if prop.name == 'name':
            self.name_label.value = self.group.name
        
    def on_mc_groups_child_update(self, **kwargs):
        #group = kwargs.get('group')
        #if getattr(group, 'id', False) not in self.MainController.Groups:
        #    return
        if 'old' in kwargs and kwargs.get('old') is None:
            return
        group = self.MainController.Groups.get(self.index)
        if group != self.group and hasattr(group, 'id') and hasattr(group, 'AttributeGroups'):
            self.set_group(group)
#        if group.index == self.index:
#            self.set_group(group)
#        elif group == self.group and group.index != self.index:
#            self.set_group(None)
